package com.guchenbo.example.security;

import org.junit.jupiter.api.Test;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
import java.security.KeyStore;

/**
 * @Author gucb
 * @Date 15/09/2017.
 */
public class KeyTest {

    @Test
    public void t1() {
        try {
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            String key = System.getProperty("java.home") + "/lib/security/cacerts";
            String pass = "changeit";
            keyStore.load(new FileInputStream(key), pass.toCharArray());

            KeyManagerFactory keyManagerFactory = KeyManagerFactory
                            .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keyStore, pass.toCharArray());
            KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();

            TrustManagerFactory trustManagerFactory = TrustManagerFactory
                            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(keyStore);
            TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

            for (KeyManager keyManager : keyManagers) {
                System.out.println(keyManager);
            }
            for (TrustManager trustManager : trustManagers) {
                System.out.println(trustManager);
//                trustManager(trustManager);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
